<template>
    <div class="main">
        <div id="main">
            <div class="login">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <div class="rl-modal-header">		    
                        <h1>				
                            <router-link to="/login"><span class="active-title">登录</span></router-link>				
                            <router-link to="/register"><span>注册</span></router-link>		    
                        </h1>		    		  
                    </div>
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                            <el-form-item label="用户名" prop="name">
                                <el-input v-model="ruleForm.name"></el-input>
                            </el-form-item>
                            <el-form-item label="密码" prop="pass">
                                <el-input type="password" v-model="ruleForm.pass" auto-complete="off"></el-input>
                            </el-form-item>
                            <el-form-item style="padding-top: 15px;">
                                <el-button type="primary" @click="submitForm('ruleForm')" style="margin-right: 60px;">登录</el-button>
                                <el-button @click="resetForm('ruleForm')">重置</el-button>
                            </el-form-item>
                        </el-form>		  
                </el-tabs>
            </div>
        </div>
    </div>
</template>

<script>
import register from '@/components/Register'
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        return {
            activeName: 'first',
            ruleForm: {
                name: '',
                pass: '',
                checkPass: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入您的名称', trigger: 'blur' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                ],
                pass: [
                    { required: true, validator: validatePass, trigger: 'blur' }
                ]
            },
        };
    },
    methods: {
        //重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        //提交表单
        submitForm(formName) {
             this.$ajax.post( "/api/login_handle/", 
                this.$qs.stringify({ 
                    username: this.ruleForm.name, 
                    password: this.ruleForm.pass 
                }) 
            ) .then(response => { 
                console.log(response.data);
                if(response.data.code==1){
                    this.$router.push('index');
                    toastr.success('登录成功！')
                }else{
                    toastr.error(response.data.msg)
                }
                var username = this.ruleForm.name;
                this.$store.commit("addTo",username);
            }) 
            .catch(error => { 
                console.log('error submit!!'); 
            });
        },
    },
    components: {
        register
    }
}
</script>

<style rel="stylesheet/scss" lang="scss">

.main{
    min-width: 480px;
    background-color: #f8fafc;
    padding-top: 0;
}
#main{
    min-height: auto;
    padding: 20px 0;
}
.login {
    position: relative;
    margin: 0 auto;
    left: 0;
    top: 110px;
    z-index: 0;
    background: #fff;
    width: 384px;
    padding-top: 10px;
    padding-bottom: 30px;
    padding-right: 20px;
    box-shadow: 0 12px 24px 0 rgba(28,31,33,.1);
    border-radius: 12px;
}
.rl-modal-header {
    position: relative;
    padding: 0 20px 25px;
    h1 {
        font-size: 16px;
        line-height: 49px;
        font-weight: 700;
        height: 49px;
        color: #787d82;
        span {
            float: left;
            width: 80px;
            color: #787d82;
            text-align: center;
            height: 49px;
            line-height: 49px;
            cursor: pointer;
        }
        span:hover {
            color: #f20d0d;
        }
        span:hover::after {
            content: " ";
            width: 16px;
            height: 4px;
            line-height: 4px;
            background: #f20d0d;
            border-radius: 2px;
            display: block;
            margin: 0 auto;
        }
    }
    .active-title {
        color: #f20d0d;
    }
}
.el-tabsitem {
    text-align: center;
    width: 60px;
}
</style>